package com.lys.emsystem.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.emsystem.mapper.CollegeMapper;
import com.lys.emsystem.entity.College;
import com.lys.emsystem.entity.School;
import com.lys.emsystem.service.CollegeService;
import com.lys.emsystem.utils.Result;
import com.lys.emsystem.vo.CollegeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/college")
public class CollegeController {
    @Autowired
    private CollegeService collegeService;

    @Autowired
    private CollegeMapper collegeMapper;

    @PostMapping("/find")
    public Result searchCollegesBySchoolIds(@RequestBody School school) {
        List<College> collegeList = collegeService.searchCollegesBySchoolIds(school.getId());
        return Result.buildResult(Result.Status.OK, collegeList);
    }

    @PostMapping
    public Result search(@RequestBody CollegeVo collegeVo) {
        System.out.println(collegeVo);
        IPage<College> search = collegeService.search(
                new Page<>(collegeVo.getPageIndex(), collegeVo.getPageSize()),
                collegeVo);
        collegeVo.setTotalCount((int) search.getTotal());
        collegeVo.setColleges(search.getRecords());
        return Result.buildResult(Result.Status.OK,collegeVo);
    }

    @PostMapping("/add")
    public Result addCollege(@RequestBody College college) {
        int i = collegeService.addCollege(college);
        if (i > 0) {
            return Result.buildResult(Result.Status.OK,college);
        }else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delete")
    public Result deleteCollege(@RequestBody College college) {
        int i = collegeService.deleteCollege(college.getId());
        if (i > 0) {
            return Result.buildResult(Result.Status.OK);
        }else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/update")
    public Result updateCollege(@RequestBody College college) {
        int i = collegeService.updateCollege(college);
        if (i > 0) {
            return Result.buildResult(Result.Status.OK);
        }else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delList")
    public Result deleteList(@RequestBody CollegeVo collegeVo) {
        int i = collegeService.deleteColleges(collegeVo.getIds());
        if (i > 0) {
            return Result.buildResult(Result.Status.OK);
        }else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }


}
